/**
 * 
 */
package com.yerba.ricecooker.quote;

import com.yerba.ricecooker.utility.CommonLogger;
import com.yerba.ricecooker.utility.DataSet;

/**
 * @author mxie
 * 
 */
public class Quotes
{
    private CommonLogger logger = CommonLogger.instance();
    
    String symbol = "";

    public String getSymbol()
    {
        return symbol;
    }

    public void setSymbol(String symbol)
    {
        this.symbol = symbol;
    }

    DataSet<String, Quote> dataset = new DataSet<String, Quote>();
    DataSet<String, Dividend> dividends = new DataSet<String, Dividend>();

    public DataSet<String, Quote> getDataset()
    {
        return dataset;
    }

    public void setDataset(DataSet<String, Quote> dataset)
    {
        this.dataset = dataset;
    }

    public DataSet<String, Dividend> getDividends()
    {
        return dividends;
    }

    public void setDividends(DataSet<String, Dividend> dividends)
    {
        this.dividends = dividends;
    }

    public void add(Quote data)
    {
        dataset.insert(data.getDate(), data);
    }

    public Quote get(int index)
    {
        return dataset.getData(index);
    }

    public Quote get(String date)
    {
        return dataset.getData(date);
    }

    public int index(String date)
    {
        return dataset.getDataIndex(date);
    }

    public int size()
    {
        return dataset.getSize();
    }

    public void clear()
    {
        symbol = "";
        dataset.clear();
        dividends.clear();
    }

    public void calculateTrueClose()
    {
        int div_size = dividends.getSize();
        Dividend div = null;
        Quote quote = null;

        try
        {
            for (int i = 0; i < div_size; i++)
            {
                div = dividends.getData(div_size - i - 1);
                quote = get(div.getDate());
                if (quote == null) throw new Exception(div.toString());

                int j = index(div.getDate());
                for (; j < size(); j++)
                {
                    quote = get(j);
                    quote.setTrueClose(quote.getTrueClose() + div.getDividend());
                }
            }
        }
        catch (Exception e)
        {
            logger.log("Exception[Quotes.calculateTrueClose()]: " + e.toString());
        }
    }

}
